package com.emm.yixun.estate.service.impl;
import java.util.List;

import javax.jws.WebService;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.emm.yixun.basic.service.ProjectCarareaService;
import com.emm.yixun.basic.service.ProjectCarportService;
import com.emm.yixun.common.exception.ResponseCode;
import com.emm.yixun.common.model.ProjectCararea;
import com.emm.yixun.common.model.ProjectCarport;
import com.emm.yixun.common.model.dto.ProjectCarareaDto;
import com.emm.yixun.common.model.dto.ProjectCarportDto;
import com.emm.yixun.common.model.enums.DelFlagEnum;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.estate.service.ProjectCarareaServiceFacade;

@Service("projectCarareaServiceFacade")
@WebService(name="com.emm.yixun.estate.service.ProjectCarareaServiceFacade")
public class ProjectCarareaServiceFacadeImpl implements ProjectCarareaServiceFacade{
	private  final Logger   logger = LoggerFactory.getLogger(this.getClass()); 
	
	@Autowired
	private ProjectCarareaService service;
	@Autowired
	private ProjectCarportService projectCarportService;

	@Override
	public Response<ProjectCarareaDto> save(ProjectCarareaDto dto) {
		Response<ProjectCarareaDto> response=new Response<ProjectCarareaDto>();
		try {
			if(null==dto){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("保存"));
				return response;
			}
			ProjectCarareaDto mDto=service.create(dto);
			if(null==mDto){
				response.fail("80000", "保存数据错误");
				return response;
			}
			response.success(mDto);
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("保存数据错误：{}",e);
		}
		return response;
	}

	@Override
	public Response<Integer> updateByPrimaryKey(ProjectCarareaDto dto,Boolean selective) {
		Response<Integer> response=new Response<Integer>();
		try {
			if(null==selective || null==dto || null==dto.getId()){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("更新"));
				return response;
			}
			
			if(selective){
				int count=service.update(dto);
				//状态变为无效时,下属车位变为无效
				if(DelFlagEnum.DEL.getNo().equals(dto.getDelFlag())){
					//修改车区下车位
					ProjectCarportDto carportDto = new ProjectCarportDto();
					carportDto.setCarareaId(dto.getId());
					carportDto.setDelFlag(DelFlagEnum.DEL.getNo());
					projectCarportService.update(carportDto);
				}
				if(count>0){
					response.success(Integer.valueOf(count));
				}else{
					response.fail("90001", "更新数据失败");
				}
			}else{
				response.fail("90000", "暂未实现,修改参数selective=true即可");
			}
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("更新数据错误：{}",e);
		}
		return response;
	}

	@Override
	public Response<Integer> deleteByPrimaryKey(Long id) {
		Response<Integer> response=new Response<Integer>();
		try {
			if(null==id){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("删除"));
				return response;
			}
			
			ProjectCararea cararea = service.selectById(id);
			//修改车位
    		ProjectCarportDto carportDto = new ProjectCarportDto();
    		carportDto.setCarareaId(cararea.getId());
    		List<ProjectCarport> carportList = projectCarportService.selectListByDto(carportDto);
    		String carportIds = "";
    		for(ProjectCarport carport: carportList){
    			if(StringUtils.isNotEmpty(carportIds)){
    				carportIds += ",";
    			}
    			carportIds += carport.getId();
    		}
    		carportDto.setDelFlag(DelFlagEnum.DEL.getNo());
    		projectCarportService.updates(carportDto, carportIds);
			
			ProjectCarareaDto mDto=new ProjectCarareaDto();
			mDto.setId(id);
			int count=service.destroy(mDto);
			if(count>0){
				response.success(Integer.valueOf(count));
			}else{
				response.fail("90001", "删除数据失败");
			}
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("删除数据错误：{}",e);
		}
		return response;
	}
	
    @Override
	public Response<ProjectCararea> findById(Long id) {
		Response<ProjectCararea> response=new Response<ProjectCararea>();
		try {
			if(null==id){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("主键查询"));
				return response;
			}
			ProjectCararea entity=service.selectById(id);
			if(null==entity){
				response.fail("90001", "主键查询数据失败");
			}else{
				response.success(entity);;
			}
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("主键查询数据错误：{}",e);
		}
		return response;
	}

	@Override
	public ResponseList<ProjectCararea> findByDto(ProjectCarareaDto dto) {
		ResponseList<ProjectCararea> response=new ResponseList<ProjectCararea>();
		try {
			if(null==dto){
				response.fail(ResponseCode.PARAM_INVALID.getCode(), ResponseCode.PARAM_INVALID.formatMessage("查询"));
				return response;
			}
			List<ProjectCararea> list=service.selectListByDto(dto);
			if(null==list){
				response.fail(ResponseCode.NO_DATA_FOUND.getCode(), ResponseCode.NO_DATA_FOUND.formatMessage("查询"));
				return response;
			}
			response.success(list);
		} catch (Exception e) {
			response.fail(ResponseCode.SYSTEM_EXCEPTION.getCode(), ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("查询数据错误：{}",e);
		}
		return response;
	}

	@Override
	public Page<ProjectCararea> findPageByDto(ProjectCarareaDto dto, Integer begin,Integer row) {
		Page<ProjectCararea> response=new Page<ProjectCararea>();
		try {
			if(null==dto || null==begin || null==row){
				response.setSuccess(false);
				response.setErrorMsg("参数不合法");
				return response;
			}
			Page<ProjectCararea> page = service.getPageModel(dto, begin, row);
			if(null==page.getRows()){
				response.setSuccess(false);
				response.setErrorMsg("分页数据不存在");
				return response;
			}
			response=page;
			response.setSuccess(true);
		} catch (Exception e) {
			response.setSuccess(false);
			response.setErrorMsg(ResponseCode.SYSTEM_EXCEPTION.getMessageTemplate());
			logger.error("查询分页数据错误：{}",e);
		}
		return response;
	}

}
